package com.duang.framework.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.sql.DataSource;

public class ExcuteSQLUtils {

	/**
	 * @deprecated
	 * @param dataSource
	 * @param sql
	 * @param args
	 * @return
	 */
	public static boolean executeUpdate(DataSource dataSource, String sql,
			Object... args) {
		return executeUpdate(getConnection(dataSource), sql, args);
	}

	/**
	 * @deprecated
	 * @param connection
	 * @param sql
	 * @param args
	 * @return
	 */
	public static ResultSet getResultSet(Connection connection, String sql,
			Object... args) {
		try {
			PreparedStatement ps = getPreparedStatement(connection, sql);
			setPreparedStatementArgs(ps, args);
			return ps.executeQuery();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;

	}

	public static ResultSet getResultSet(PreparedStatement ps, String sql,
			Object... args) {
		try {
			setPreparedStatementArgs(ps, args);
			return ps.executeQuery();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}

	public static void setPreparedStatementArgs(PreparedStatement ps,
			Object... args) throws SQLException {
		for (int i = 1; i <= args.length; i++) {
			ps.setObject(i, args[i - 1]);
		}

	}

	public static boolean executeUpdate(PreparedStatement ps,
			Object... args) {
		try {
			setPreparedStatementArgs(ps, args);
			ps.executeUpdate();
			return true;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return false;
	}

	/**
	 * @deprecated
	 * @param connection
	 * @param sql
	 * @param args
	 * @return
	 */
	public static boolean executeUpdate(Connection connection, String sql,
			Object... args) {
		try {
			PreparedStatement ps = getPreparedStatement(connection, sql);
			setPreparedStatementArgs(ps, args);
			ps.executeUpdate();
			return true;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return false;
	}

	public static PreparedStatement getPreparedStatementStripException(
			Connection conn, String sql) {
		try {
			return conn.prepareStatement(sql);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}

	public static void close(Connection conn, PreparedStatement ps,
			ResultSet rs) throws DuangDaoException{
		close(conn);
		close(ps);
		close(rs);

	}

	public static void close(ResultSet rs) throws DuangDaoException{
		if (rs != null) {
			try {
				rs.close();
			} catch (SQLException e) {
				throw new DuangDaoException(e);
			}
		}

	}

	public static void close(PreparedStatement ps) 	throws DuangDaoException{
		if (ps != null) {
			try {
				ps.close();
			} catch (SQLException e) {
				throw new DuangDaoException(e);
			}
		}

	}

	public static void close(Connection connection) throws DuangDaoException{
		if (connection != null) {
			try {
				connection.close();
			} catch (SQLException e) {
				throw new DuangDaoException(e);
			}
		}

	}

	public static PreparedStatement getPreparedStatement(Connection conn,
			String sql) throws SQLException {
		return conn.prepareStatement(sql);
	}

	/**
	 * @deprecated Using @getPreparedStatement(Connectioni conn,String sql)
	 *             replace
	 * @param dataSource
	 * @param sql
	 * @return
	 * @throws SQLException
	 */
	public static PreparedStatement getPreparedStatement(DataSource dataSource,
			String sql) throws SQLException {
		Connection conn = getConnection(dataSource);
		return conn.prepareStatement(sql);
	}

	public static Connection getConnection(DataSource dataSource) {
		try {
			return dataSource.getConnection();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}

}
